﻿Imports System.Web
Imports DotNetNuke.Modules.ActiveForums.Data
Imports Microsoft.ApplicationBlocks.Data
Imports DotNetNuke.Services.Journal
Namespace DotNetNuke.Modules.ActiveForums
    Public Class Social
        Inherits DataConfig
        Public Function GetActiveSocialStatus(ByVal PortalId As Integer) As Integer
#If SKU = "LITE" Then
            'Not Available
            Return -2
#Else
            If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                Dim mc As New DotNetNuke.Entities.Modules.ModuleController
                Dim tc As New DotNetNuke.Entities.Tabs.TabController
                Dim ti As DotNetNuke.Entities.Tabs.TabInfo = Nothing
                For Each mi As DotNetNuke.Entities.Modules.ModuleInfo In mc.GetModules(PortalId)
                    If mi.ModuleName.Contains("Active Social") And mi.IsDeleted = False Then
                        ti = tc.GetTab(mi.TabID, PortalId, False)
                        If Not ti Is Nothing Then
                            If ti.IsDeleted = False Then
                                'Installed and ready
                                Return 1
                            End If
                        End If
                    End If
                Next
                'Not installed on portal
                Return 0
            Else
                'Not Installed
                Return -1
            End If
#End If
        End Function
        Public Sub AddTopicToJournal(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, TopicId As Integer, ByVal UserId As Integer, ByVal URL As String, ByVal Subject As String, ByVal Summary As String, ByVal Body As String, ByVal SecurityOption As Integer, ByVal ReadRoles As String, SocialGroupId As Integer)

            Dim ji As New JournalItem
            ji.PortalId = PortalId
            ji.ProfileId = UserId
            ji.UserId = UserId
            ji.Title = Subject
            ji.ItemData = New ItemData()
            ji.ItemData.Url = URL
            If (String.IsNullOrEmpty(Summary)) Then
                Summary = Utilities.StripHTMLTag(Body)
                If Summary.Length > 150 Then
                    Summary = Summary.Substring(0, 150) & "..."
                End If
            End If
            ji.Summary = Summary
            ji.Body = Body
            ji.JournalTypeId = 5
            ji.ObjectKey = String.Format("{0}:{1}", ForumId.ToString(), TopicId.ToString())
            If Not (JournalController.Instance.GetJournalItemByKey(PortalId, ji.ObjectKey) Is Nothing) Then
                JournalController.Instance.DeleteJournalItemByKey(PortalId, ji.ObjectKey)
            End If
            Dim roles As String = String.Empty
            If Not String.IsNullOrEmpty(ReadRoles) Then
                If ReadRoles.Contains("|") Then
                    roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1)
                End If
            End If

            For Each s As String In roles.Split(CChar(";"))
                If (s = "-1") Or (s = "-3") Then
                    If Not (ji.SecuritySet.Contains("E,")) Then
                        ji.SecuritySet &= "E,"
                    End If
                Else
                    ji.SecuritySet &= "R" & s & ","
                End If

            Next
            If SocialGroupId > 0 Then

                ji.SocialGroupId = SocialGroupId

            End If
            JournalController.Instance.SaveJournalItem(ji, -1)


            'If GetActiveSocialStatus(PortalId) < 1 Then
            '    Exit Sub
            'End If
            'Try
            '    Dim ai As Object = Nothing
            '    Dim asm As System.Reflection.Assembly
            '    asm = System.Reflection.Assembly.Load("Active.Modules.Social")
            '    Dim ac As Object = Nothing
            '    ac = asm.CreateInstance("Active.Modules.Social.API.Journal")
            '    Dim MainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            '    Dim roles As String = String.Empty
            '    If Not String.IsNullOrEmpty(ReadRoles) Then
            '        If ReadRoles.Contains("|") Then
            '            roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1)
            '        End If
            '    End If
            '    ac.AddProfileItem(New Guid(MainSettings.ActiveSocialTopicsKey), UserId, URL, Subject, Summary, Body, SecurityOption, roles)
            'Catch ex As Exception

            'End Try
        End Sub
        Public Sub AddReplyToJournal(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, TopicId As Integer, ReplyId As Integer, ByVal UserId As Integer, ByVal URL As String, ByVal Subject As String, ByVal Summary As String, ByVal Body As String, ByVal SecurityOption As Integer, ByVal ReadRoles As String, SocialGroupId As Integer)

            Dim ji As New JournalItem
            ji.PortalId = PortalId
            ji.ProfileId = UserId
            ji.UserId = UserId
            ji.Title = Subject
            ji.ItemData = New ItemData()
            ji.ItemData.Url = URL
            If (String.IsNullOrEmpty(Summary)) Then
                Summary = Utilities.StripHTMLTag(Body)
                If Summary.Length > 150 Then
                    Summary = Summary.Substring(0, 150) & "..."
                End If
            End If
            ji.Summary = Summary
            ji.Body = Body
            ji.JournalTypeId = 6
            ji.ObjectKey = String.Format("{0}:{1}:{2}", ForumId.ToString(), TopicId.ToString(), ReplyId.ToString())
            If Not (JournalController.Instance.GetJournalItemByKey(PortalId, ji.ObjectKey) Is Nothing) Then
                JournalController.Instance.DeleteJournalItemByKey(PortalId, ji.ObjectKey)
            End If
            Dim roles As String = String.Empty
            If Not String.IsNullOrEmpty(ReadRoles) Then
                If ReadRoles.Contains("|") Then
                    roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1)
                End If
            End If

            For Each s As String In roles.Split(CChar(";"))
                If (s = "-1") Or (s = "-3") Then
                    If Not (ji.SecuritySet.Contains("E,")) Then
                        ji.SecuritySet &= "E,"
                    End If
                Else
                    ji.SecuritySet &= "R" & s & ","
                End If

            Next
            If SocialGroupId > 0 Then
                ji.SocialGroupId = SocialGroupId
            End If
            JournalController.Instance.SaveJournalItem(ji, -1)
          
        End Sub
        Public Sub AddForumItemToJournal(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal entryType As String, ByVal sURL As String, ByVal sSubject As String, ByVal sBody As String)
            Try
#If SKU <> "LITE" Then
                Dim MainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) And MainSettings.ProfileType = ProfileTypes.Social Then
                    Dim ai As Object = Nothing
                    Dim asm As System.Reflection.Assembly
                    asm = System.Reflection.Assembly.Load("Active.Modules.Social")
                    Dim ac As Object = Nothing
                    Dim groupId As Integer = -1
                    If Not HttpContext.Current.Request.Params("asg") Is Nothing Then
                        groupId = CInt(HttpContext.Current.Request.Params("asg"))
                    End If
                    If groupId > 0 Then
                        ac = asm.CreateInstance("Active.Modules.Social.JournalController")
                        If Not ac Is Nothing Then
                            ac.JournalAddForumItem(UserId, groupId, PortalId, entryType, sURL, sSubject, sBody)
                        End If
                    Else

                    End If

                End If
#End If

            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
        End Sub
        Friend Function ActiveSocialListGroups(ByVal PortalId As Integer) As IDataReader
#If SKU <> "LITE" Then
            If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                Dim sSQL As String = "Select g.GroupId, g.GroupName from " & _databaseOwner & _objectQualifier & "activesocial_Groups as g WHERE PortalId = " & PortalId.ToString & " AND StatusId = 1"
                Return SqlHelper.ExecuteReader(_connectionString, CommandType.Text, sSQL)
            Else
                Return Nothing
            End If

#Else
            Return nothing
#End If
        End Function
        Friend Function ActiveSocialGroups(ByVal userId As Integer, ByVal PortalId As Integer) As String
#If SKU <> "LITE" Then
            Dim sGroups As String = String.Empty
            Try
                'Dim MainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                    Dim ds As DataSet = SqlHelper.ExecuteDataset(_connectionString, _databaseOwner & _objectQualifier & "activesocial_Groups_ListMyGroups", PortalId, 0, 1000, userId, 1, userId)
                    If ds.Tables.Count > 0 Then
                        Dim dt As DataTable = ds.Tables(0)
                        If dt.Rows.Count > 0 Then
                            For Each dr As DataRow In dt.Rows
                                sGroups &= dr("GroupId").ToString & ":" & dr("GroupMemberTypeId").ToString & ";"
                            Next
                        End If
                    Else
                        Return String.Empty
                    End If
                Else
                    Return String.Empty
                End If
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Return sGroups
#Else
            Return String.Empty
#End If

        End Function
        Friend Function ActiveSocialGetGroup(ByVal groupId As Integer) As String
#If SKU <> "LITE" Then
            Dim sGroups As String = String.Empty
            Try
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                    sGroups = CType(SqlHelper.ExecuteScalar(_connectionString, _databaseOwner & _objectQualifier & "activesocial_Groups_GetName", groupId), String)
                    Return sGroups
                End If
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Return sGroups
#Else
            Return String.Empty
#End If
        End Function
        Friend Function ActiveSocialGetGroupType(ByVal groupTypeId As Integer, ByVal PortalId As Integer) As String
#If SKU <> "LITE" Then
            Dim sGroups As String = String.Empty
            Try
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                    Using dr As IDataReader = SqlHelper.ExecuteReader(_connectionString, _databaseOwner & _objectQualifier & "activesocial_Groups_GetMemberTypes", PortalId)
                        While dr.Read
                            If CInt(dr("GroupMemberTypeId").ToString) = groupTypeId Then
                                sGroups = dr("MemberTypeName").ToString

                            End If
                        End While
                        dr.Close()
                    End Using
                    Return sGroups
                End If
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
                'Return ex.Message
            End Try
            Return sGroups
#Else
            Return String.Empty
#End If
        End Function
        Friend Function GetMemberTabId(ByVal PortalId As Integer) As Integer
#If SKU <> "LITE" Then
            Try
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                    Dim sSQL As String = "SELECT SettingValue FROM " & _databaseOwner & _objectQualifier & "activesocial_Settings WHERE SettingName='MEMBERSTABID' AND PortalId = " & PortalId
                    Return CType(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL), Integer)
                End If
                Return -1
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Return -1
#Else
            Return -1
#End If

        End Function
        Friend Function GetMasterForumGroup(ByVal PortalId As Integer, ByVal CurrentTabId As Integer) As Integer
            Dim ForumGroupId As Integer = -1
            Dim GroupViewTabId As Integer = -1
#If SKU <> "LITE" Then
            Try
                If IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")) Then
                    Dim sSQL As String = "SELECT SettingValue FROM " & _databaseOwner & _objectQualifier & "activesocial_Settings WHERE SettingName='GLOBALGROUPFORUMTEMPLATEGROUP' AND PortalId = " & PortalId
                    ForumGroupId = CType(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL), Integer)
                    sSQL = "SELECT SettingValue FROM " & _databaseOwner & _objectQualifier & "activesocial_Settings WHERE SettingName='GROUPSVIEWTABID' AND PortalId = " & PortalId
                    GroupViewTabId = CType(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL), Integer)
                    If GroupViewTabId = CurrentTabId Then
                        ForumGroupId = -1
                    End If
                End If
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
#End If
            Return ForumGroupId
        End Function
    End Class
End Namespace
